package com.ry.poi;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class Test {

	public static void main(String[] args) {
		importCsv();
	}

	public static void importCsv() {
		
		File file = new File("E:/");
		File []fs = file.listFiles();
		for (File f : fs) {
			if(f.isFile()) {
				String fileName = f.getName();
				if(fileName.startsWith("1440078039488_200716")) {
					
					List<String> dataList = Test.importCsv(f);
					if (dataList != null && !dataList.isEmpty()) {
						for (int i = 0; i < dataList.size(); i++) {
							if (i != 0) {// 不读取第一行
								String row = dataList.get(i);
								System.out.println(row);
								String[] cols = row.split(",");
								String date = cols[0].trim();
								String time = cols[1].trim();
								String isLeak = cols[2].trim();
								String niose = cols[3].trim();
								String spread = cols[4].trim();
								String voltage = cols[5].trim();
								String signal = cols[6].trim();
								
								//日期格式转换
								DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yy/MM/dd HH:mm:ss");
								DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
								LocalDateTime s = LocalDateTime.parse(date+" "+time, dtf);
								String tamp = s.format(dtf2);	//采集时间
								System.out.println(tamp);
								
								System.out.println(Double.valueOf(isLeak).intValue());
								System.out.println(niose);
								System.out.println(spread);
								System.out.println(voltage);
								System.out.println(signal);
								
							}
						}
					}
					
				}
			}
			
		}
		
	}

	public static List<String> importCsv(File file) {
		List<String> dataList = new ArrayList<String>();

		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(file));
			String line = "";
			while ((line = br.readLine()) != null) {
				dataList.add(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
					br = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return dataList;
	}

	public static boolean exportCsv(File file, List<String> dataList) {
		boolean isSucess = false;
		FileOutputStream out = null;
		OutputStreamWriter osw = null;
		BufferedWriter bw = null;
		try {
			out = new FileOutputStream(file);
			osw = new OutputStreamWriter(out);
			bw = new BufferedWriter(osw);
			if (dataList != null && !dataList.isEmpty()) {
				for (String data : dataList) {
					bw.append(data).append("\r");
				}
			}
			isSucess = true;
		} catch (Exception e) {
			isSucess = false;
		} finally {
			if (bw != null) {
				try {
					bw.close();
					bw = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (osw != null) {
				try {
					osw.close();
					osw = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (out != null) {
				try {
					out.close();
					out = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return isSucess;
	}
}
